﻿#pragma once
// Copyright (c) lanyeo 2020-2021
// Licenced under MIT license. See https://opensource.org/licenses/MIT for details.
// code style
// 1. use forward declaration (尽可能使用前置声明)
// 2. include file sequence [other project>c>c++>third>self project]
// 


#define INVALID_TOKEN service_token_t(0, 0)
#define make_token(TYPE, ID) service_token_t(TYPE, ID)

#define self_token service_token_t(service_type(), service_id())
#define _any_lobby service_token_t(BOX_TYPE_LOBBY, BOX_TYPE_INVALID)
#define _any_transmit service_token_t(BOX_TYPE_TRANSMIT, BOX_TYPE_INVALID)
#define _global service_token_t(BOX_TYPE_GLOBAL, BOX_TYPE_INVALID)


#include "rpc.pb.h"
struct service_token_t
{
    service_token_t()
        : type(0), id(0)
    {
        
    }
    service_token_t(const protobuf_service_token& token)
        : type(token.type()), id(token.id())
    {

    }
    service_token_t(int _type, int _id)
        : type(_type), id(_id)
    {

    }
    service_token_t(const service_token_t& token)
        : type(token.type), id(token.id)
    {

    }
    service_token_t(const service_token_t&& token) noexcept
        : type(token.type), id(token.id)
    {

    }

    bool operator==(const service_token_t& token) const
    {
        return type == token.type && id == token.id;
    }
    
    int32_t type;
    int32_t id;

    struct HashFunc
    {
        std::size_t operator()(const service_token_t& key) const
        {
            using std::size_t;
            using std::hash;

            return ((hash<int>()(key.type)
                ^ (hash<int>()(key.id) << 1)) >> 1);
        }
    };
    struct EqualKey
    {
        bool operator () (const service_token_t& lhs, const service_token_t& rhs) const
        {
            return lhs.type == rhs.type
                && lhs.id == rhs.id;
        }
    };
};
